﻿@page "/analysis"

@if (iv == null)
{
    <Loading />
}
else
{
    <div class="post-wrap">
        <div class="mta-box">
            <div class="plus-col-xs12 plus-col-md3">
                <div class="mta-item">
                    <div class="mta-title">
                        浏览量(PV)
                    </div>
                    <div class="mta-value">
                        <strong>@pv</strong>
                    </div>
                    <div class="mta-date">@today</div>
                </div>
            </div>
            <div class="plus-col-xs12 plus-col-md3">
                <div class="mta-item">
                    <div class="mta-title">
                        独立用户(UV)
                    </div>
                    <div class="mta-value">
                        <strong>@uv</strong>
                    </div>
                    <div class="mta-date">@today</div>
                </div>
            </div>
            <div class="plus-col-xs12 plus-col-md3">
                <div class="mta-item">
                    <div class="mta-title">
                        访问次数(VV)
                    </div>
                    <div class="mta-value">
                        <strong>@vv</strong>
                    </div>
                    <div class="mta-date">@today</div>
                </div>
            </div>
            <div class="plus-col-xs12 plus-col-md3">
                <div class="mta-item">
                    <div class="mta-title">
                        独立IP
                    </div>
                    <div class="mta-value">
                        <strong>@iv</strong>
                    </div>
                    <div class="mta-date">@today</div>
                </div>
            </div>
        </div>
    </div>
}

@using System.Text.Json;

@code {
    private string today = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");

    private string pv, uv, vv, iv;

    protected async override Task OnInitializedAsync()
    {
        await Common.SetTitleAsync("访问数据分析");

        var stream = await Http.GetStreamAsync($"/mta/ctr_core_data?start_date={today}&end_date={today}&idx=pv,uv,vv,iv");

        using (var json = await JsonDocument.ParseAsync(stream))
        {
            var data = json.RootElement.GetProperty("data").GetProperty(today.Replace("-", ""));

            pv = data.GetProperty("pv").GetString();
            uv = data.GetProperty("uv").GetString();
            vv = data.GetProperty("vv").GetString();
            iv = data.GetProperty("iv").GetString();
        }
    }
}